package com.example.mytips.utils;

import com.example.mytips.demos.HelloInsertCode;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ReadResourceFileUtil {
    /**
     * 读取resources目录下的文件
     * @param path 文件目录
     * @return
     */
    public static StringBuilder readFile(String path) {
        StringBuilder stringBuilder = new StringBuilder();
        // 加载资源文件
        ClassLoader classLoader = MyAnAction.class.getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream(path);
        if (inputStream != null) {
            // 读取文件内容
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line).append("\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("资源文件未找到");
        }

        return stringBuilder;
    }
}
